package com.htc.callcare.datacontroller;

import java.io.File;


import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.htc.callcare.R;
import com.htc.callcare.activities.RecordActivity;
import com.htc.callcare.activities.VoiceMailActivity;
import com.htc.callcare.adapter.RecordAdapter;
import com.htc.callcare.database.Database;
import com.htc.callcare.infrastructure.globalVar;
import com.htc.callcare.model.recordModel;

public class RecordController {
	RecordActivity activity;
	recordModel model;
	Database database;

	public RecordController(RecordActivity activity) {
		super();
		this.activity = activity;
		database = new Database(activity);
	}

	public recordModel getModel() {
		return model;
	}

	public void setModel(recordModel model) {
		this.model = model;
	}

	public void detete(RecordActivity activity, recordModel processModel,  RecordAdapter adapter) {
		// TODO Auto-generated method stub
		Log.e("count all record", ""+database.countAllRecordCheck());
		if(database.countAllRecordCheck()!=0){
			for(recordModel record : adapter.getRecords()){
				if(record.getSeparator()==0){
					if(database.findRecordCheck(record.getFileUri())){
						database.deleteCheckRecord(record.getFileUri());
						database.deleteRecordNew(record.getFileUri());
						File file = new File(globalVar.recordDir + record.getFileUri());
						file.delete();
					}
				}
			}
		}
		else{
			File file=new File(globalVar.recordDir+processModel.getFileUri());
			file.delete();
			database.deleteRecordNew(processModel.fileUri);
		}
		adapter.notifyDataSetChanged();
		activity.finish();
		activity.startActivity(new Intent(activity, RecordActivity.class));
	}
	public void send(recordModel processModel){	
		Uri uri = Uri.fromFile(new File(globalVar.recordDir, processModel.getFileUri()));
		Intent it = new Intent(Intent.ACTION_SEND);   
		it.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.recordFile)+" "+  processModel.getCallNumber());   
		it.putExtra(Intent.EXTRA_TEXT, activity.getString(R.string.date)+processModel.getCallDay()+"   "+activity.getString(R.string.time)+processModel.getCallTime()); 
		it.putExtra(Intent.EXTRA_STREAM, uri);
		it.setType("audio/rfc822");   
		activity.startActivity(Intent.createChooser(it,activity.getString(R.string.share))); 
	}
	
	public void deleteItem(recordModel processModel){
		File file=new File(globalVar.recordDir+processModel.getFileUri());
		file.delete();
	}

	

}
